<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能仓储管理系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.1/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.8/dist/chart.umd.min.js"></script>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f8f9fa;
        }
        .card {
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s, box-shadow 0.3s;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.12);
        }
        .card-header {
            background-color: #343a40;
            color: white;
            font-weight: bold;
        }
        .sidebar {
            height: 100vh;
            position: fixed;
            z-index: 1;
            top: 0;
            left: 0;
            background-color: #343a40;
            padding-top: 20px;
        }
        .sidebar a {
            padding: 15px 20px;
            text-decoration: none;
            font-size: 18px;
            color: #adb5bd;
            display: block;
            transition: 0.3s;
        }
        .sidebar a:hover {
            color: white;
            background-color: #495057;
        }
        .main-content {
            margin-left: 250px;
            padding: 20px;
        }
        .stat-card {
            text-align: center;
            padding: 20px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .stat-number {
            font-size: 36px;
            font-weight: bold;
            color: #343a40;
        }
        .stat-label {
            font-size: 16px;
            color: #6c757d;
        }
        .chart-container {
            margin-top: 30px;
        }
        @media (max-width: 768px) {
            .sidebar {
                width: 100%;
                height: auto;
                position: relative;
            }
            .main-content {
                margin-left: 0;
            }
        }
    </style>
</head>
<body>
    <div class="sidebar col-md-2">
        <div class="text-center mb-5">
            <h2 class="text-white">智能仓储</h2>
        </div>
        <a href="/">首页</a>
        <a href="/items">物品管理</a>
        <a href="/locations">位置管理</a>
        <a href="/logs">出入库记录</a>
    </div>

    <div class="main-content">
        <div class="container-fluid">
            <h1 class="mb-4">智能仓储管理系统</h1>
            
            <!-- 统计卡片 -->
            <div class="row mb-5">
                <div class="col-md-3">
                    <div class="stat-card">
                        <div class="stat-number">{{ total_items }}</div>
                        <div class="stat-label">物品种类</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stat-card">
                        <div class="stat-number">{{ total_quantity }}</div>
                        <div class="stat-label">总库存数量</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stat-card">
                        <div class="stat-number">{{ reorder_items }}</div>
                        <div class="stat-label">需要补货</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stat-card">
                        <button class="btn btn-primary" id="addItemBtn">添加物品</button>
                    </div>
                </div>
            </div>

            <!-- 图表区域 -->
            <div class="row chart-container">
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header">物品类别分布</div>
                        <div class="card-body">
                            <canvas id="categoryChart"></canvas>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header">一周出入库趋势</div>
                        <div class="card-body">
                            <canvas id="trendChart"></canvas>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 最近记录 -->
            <div class="row mt-5">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-header">最近出入库记录</div>
                        <div class="card-body">
                            <table class="table table-striped">
                                <thead>
                                    <tr>
                                        <th>时间</th>
                                        <th>物品名称</th>
                                        <th>操作</th>
                                        <th>数量</th>
                                        <th>位置</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for log in recent_logs %}
                                    <tr>
                                        <td>{{ log[7] }}</td>
                                        <td>{{ log[2] }}</td>
                                        <td>
                                            <span class="badge {{ 'badge-success' if log[3] == 'IN' else 'badge-danger' }}">
                                                {{ '入库' if log[3] == 'IN' else '出库' }}
                                            </span>
                                        </td>
                                        <td>{{ log[4] }}</td>
                                        <td>{{ log[5] }}</td>
                                    </tr>
                                    {% else %}
                                    <tr>
                                        <td colspan="5" class="text-center">暂无记录</td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                            <a href="/logs" class="btn btn-secondary">查看全部记录</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 添加物品模态框 -->
    <div class="modal fade" id="addItemModal" tabindex="-1" role="dialog" aria-labelledby="addItemModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addItemModalLabel">添加新物品</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="addItemForm">
                        <div class="form-group">
                            <label for="tag_id">RFID标签ID</label>
                            <input type="text" class="form-control" id="tag_id" required>
                        </div>
                        <div class="form-group">
                            <label for="name">物品名称</label>
                            <input type="text" class="form-control" id="name" required>
                        </div>
                        <div class="form-group">
                            <label for="description">物品描述</label>
                            <textarea class="form-control" id="description"></textarea>
                        </div>
                        <div class="form-group">
                            <label for="category">类别</label>
                            <input type="text" class="form-control" id="category">
                        </div>
                        <div class="form-group">
                            <label for="quantity">数量</label>
                            <input type="number" class="form-control" id="quantity" min="1" value="1" required>
                        </div>
                        <div class="form-group">
                            <label for="location">位置</label>
                            <select class="form-control" id="location" required>
                                <option value="货架A1">货架A1</option>
                                <option value="货架A2">货架A2</option>
                                <option value="货架B1">货架B1</option>
                                <option value="货架B2">货架B2</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="unit_price">单价</label>
                            <input type="number" step="0.01" class="form-control" id="unit_price" min="0" value="0">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="submitAddItem">添加</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 初始化图表
        $(document).ready(function() {
            // 添加物品按钮点击事件
            $('#addItemBtn').click(function() {
                $('#addItemModal').modal('show');
            });

            // 提交添加物品表单
            $('#submitAddItem').click(function() {
                const formData = {
                    tag_id: $('#tag_id').val(),
                    name: $('#name').val(),
                    description: $('#description').val(),
                    category: $('#category').val(),
                    quantity: parseInt($('#quantity').val()),
                    location: $('#location').val(),
                    unit_price: parseFloat($('#unit_price').val())
                };

                $.ajax({
                    url: '/api/add_item',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(formData),
                    success: function(response) {
                        if (response.success) {
                            alert('物品添加成功！');
                            $('#addItemModal').modal('hide');
                            location.reload();
                        } else {
                            alert('添加失败：' + response.message);
                        }
                    },
                    error: function() {
                        alert('请求失败，请重试。');
                    }
                });
            });

            // 获取库存分析数据并绘制图表
            $.ajax({
                url: '/api/inventory_analysis',
                type: 'GET',
                success: function(data) {
                    // 绘制物品类别分布图表
                    if (data.categories.length > 0) {
                        const categoryCtx = document.getElementById('categoryChart').getContext('2d');
                        new Chart(categoryCtx, {
                            type: 'pie',
                            data: {
                                labels: data.categories,
                                datasets: [{
                                    data: data.category_counts,
                                    backgroundColor: [
                                        '#FF6384',
                                        '#36A2EB',
                                        '#FFCE56',
                                        '#4BC0C0',
                                        '#9966FF',
                                        '#FF9F40'
                                    ]
                                }]
                            },
                            options: {
                                responsive: true,
                                plugins: {
                                    legend: {
                                        position: 'right'
                                    }
                                }
                            }
                        });
                    }

                    // 绘制一周出入库趋势图表
                    if (data.dates.length > 0) {
                        const trendCtx = document.getElementById('trendChart').getContext('2d');
                        new Chart(trendCtx, {
                            type: 'line',
                            data: {
                                labels: data.dates,
                                datasets: [
                                    {
                                        label: '入库数量',
                                        data: data.in_data,
                                        borderColor: '#4CAF50',
                                        backgroundColor: 'rgba(76, 175, 80, 0.1)',
                                        tension: 0.1
                                    },
                                    {
                                        label: '出库数量',
                                        data: data.out_data,
                                        borderColor: '#f44336',
                                        backgroundColor: 'rgba(244, 67, 54, 0.1)',
                                        tension: 0.1
                                    }
                                ]
                            },
                            options: {
                                responsive: true,
                                scales: {
                                    y: {
                                        beginAtZero: true,
                                        ticks: {
                                            stepSize: 1
                                        }
                                    }
                                }
                            }
                        });
                    }
                },
                error: function() {
                    console.log('获取库存分析数据失败');
                }
            });
        });
    </script>
</body>
</html>